<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /homepage/default.php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
# 载入常用集
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 60;

class page_index  extends page{
	function __construct(){
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
		$this->listview = array();
	}
	function len(){
		$l;
		try{
			$sql = "
				SELECT COUNT(*) AS cnt
				FROM cs2_hsell;";
			$l = $this->database->select($sql);
		}catch (Exception $e) {
			$l = null;
		}
		return $l[0]['cnt'];
	}
	function prod_listview(){
		$lv = null;
		try{
			$sql = "SELECT prodId,name,model 
				FROM cs2_prod ORDER BY model ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}
	function listview(){
		$lv = null;
		try{
			$sql = "SELECT hsellId,cs2_hsell.prodId,icon,iconinfo,num,contents,price,sprice,cs2_hsell.adminId,
						cs2_prod.name,cs2_prod.model,cs2_hsell.title,
				IFNULL((SELECT cs2_image.fileId FROM cs2_image WHERE 
					cs2_image.prodId=cs2_prod.prodId AND cs2_image.defId=0 LIMIT 0,1),'no_image') AS fileId
				FROM cs2_hsell LEFT JOIN cs2_prod ON (cs2_prod.prodId = cs2_hsell.prodId)
				ORDER BY hsellId ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}
	function ajax_callback(){
		global $config;
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		
		if($token == config::TOKEN_ADD_HSELL){
			$icon = $_POST['icon'];
			$pro = $_POST['pro'];
			$title = $_POST['title'];
			$iconinfo = $_POST['iconinfo'];
			$num = $_POST['num'];
			$contents = $_POST['contents'];
			$price = $_POST['price'];
			$sprice = $_POST['sprice'];
			$adminId = $_POST['adminId'];
			
			$sql = "INSERT INTO cs2_hsell (prodId, icon, iconinfo, num, contents, price, sprice,adminId,title) 
			VALUES (".$pro.",".$icon.",'".$iconinfo."',".$num.",'".$contents."',".$price.",".$sprice.",".$adminId.",'".$title."');";
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_UP_HSELL){
			$icon = $_POST['icon'];
			$pro = $_POST['pro'];
			$title = $_POST['title'];
			$iconinfo = $_POST['iconinfo'];
			$num = $_POST['num'];
			$contents = $_POST['contents'];
			$price = $_POST['price'];
			$sprice = $_POST['sprice'];
			$adminId = $_POST['adminId'];
			$hsellId = $_POST['hsellId'];
			
			$sql = "UPDATE cs2_hsell SET 
				icon=".$icon." ,
				prodId=".$pro." ,
				title='".$title."' ,
				iconinfo='".$iconinfo."' ,
				num=".$num." ,
				contents='".$contents."' ,
				price=".$price." ,
				sprice=".$sprice." ,
				adminId=".$adminId." 
				WHERE (hsellId =". $hsellId.");";

			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_DEL_HSELL){
			$hsellId = $_POST['hsellId'];
			$sql = "DELETE FROM cs2_hsell WHERE (hsellId=".$hsellId.")";
			try {
				$this->database->delete($sql);
				$json = array("ref"=>"OK","name");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		
		return json_encode($json);
	}
}
$pg = new page_index();
## Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('prod_listview', $pg->prod_listview());
	$config->smarty->assign('listview', $pg->listview());
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}



?>